package org.itt;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.concurrent.TimeUnit;

import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Logger;

// Final, because it's not supposed to be sub classed
public final class FooUtil {
        private static Logger logger = Logger.getLogger("FooUtil");

        private static final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();

        // Private constructor to avoid unnecessary instantiation of the class
        private FooUtil() {
        }

        public static String prettyPrintTime(long elapsedTime) {
                long hours = TimeUnit.HOURS.convert(elapsedTime, TimeUnit.NANOSECONDS);
                long minutes = TimeUnit.MINUTES.convert(elapsedTime,
                                TimeUnit.NANOSECONDS) % 60;
                long seconds = TimeUnit.SECONDS.convert(elapsedTime,
                                TimeUnit.NANOSECONDS) % 60;
                long milliseconds = TimeUnit.MILLISECONDS.convert(elapsedTime,
                                TimeUnit.NANOSECONDS) % 1000;
                long microseconds = TimeUnit.MICROSECONDS.convert(elapsedTime,
                                TimeUnit.NANOSECONDS) % 1000;

                return (hours + " ore, " + minutes + " minuti, " + seconds
                                + " secondi, " + milliseconds + " millisecondi, "
                                + microseconds + " microsecondi");
        }

        public static String calculateHash(byte[] file) {
                String error = null;
                MessageDigest digest = null;
                String hash = null;

                try {
                        switch (Properties.get().INDEX_HASH_TYPE) {
                        case "SHA1":
                                switch (Properties.get().INDEX_HASH_TYPE_CODE) {
                                case "HEX":
                                        error = "Impossibile creare un MessageDigest per SHA1";
                                        digest = MessageDigest.getInstance("SHA-1");

                                        // Update input string in message digest
                                        error = "Errore durante la chiamata update()";
                                        digest.update(file, 0, file.length);

                                        // Converts message digest value in HEX
                                        hash = toHex(digest.digest());
                                        break;

                                case "BASE64":
                                        error = "Impossibile creare un MessageDigest per SHA1";
                                        digest = MessageDigest.getInstance("SHA-1");

                                        // Update input string in message digest
                                        digest.update(file, 0, file.length);

                                        // Converts message digest value in Base64
                                        /* VERIFICARE */
                                        hash = new String(Base64.encodeBase64(new BigInteger(1, digest
                                                        .digest()).toByteArray()));

                                        // hash = new
                                        // String(Base64.encodeBase64(toHex(digest.digest()).getBytes()));

                                        break;

                                default:
                                }
                                break;

                        case "SHA256":
                                switch (Properties.get().INDEX_HASH_TYPE_CODE) {
                                case "HEX":
                                        error = "Impossibile creare un MessageDigest per SHA256";
                                        digest = MessageDigest.getInstance("SHA-256");

                                        // Update input string in message digest
                                        digest.update(file, 0, file.length);

                                        // Converts message digest value in base HEX
                                        hash = toHex(digest.digest());
                                        break;

                                case "BASE64":
                                        error = "Impossibile creare un MessageDigest per SHA256";
                                        digest = MessageDigest.getInstance("SHA-256");

                                        // Update input string in message digest
                                        digest.update(file, 0, file.length);

                                        // Converts message digest value in Base64
                                        /* VERIFICARE */
                                        hash = new String(Base64.encodeBase64(new BigInteger(1, digest
                                                        .digest()).toByteArray()));

                                        // hash = new
                                        // String(Base64.encodeBase64(toHex(digest.digest()).getBytes()));

                                        break;
                                }
                                break;
                        default:
                        }
                } catch (Exception e) {
                        logger.error(e + ": " + error);
                }

                return hash;
        }

        private static String toHex(byte[] data) {
                char[] chars = new char[data.length * 2];
                for (int i = 0; i < data.length; i++) {
                        chars[i * 2] = HEX_DIGITS[(data[i] >> 4) & 0xf];
                        chars[i * 2 + 1] = HEX_DIGITS[data[i] & 0xf];
                }
                return new String(chars);
        }
}
